"""
将（quary，gallery）索引矩阵，转换成json图片名称矩阵
"""
import numpy as np
from collections import defaultdict
import json
from datasets.mydata import mydata
"------------------mydata一定要把mydata.py中的flag_test改成True----------------"
"用什么数据就改成哪个数据读取类"
dataset = mydata()

query = dataset.query_normal
gallery = dataset.gallery_normal
indices = np.loadtxt('../json_save/distance_matrix').astype('int64')
(num_q, num_g) = indices.shape

index_dic = defaultdict(list)
# index_dic = {}
for q_idx in range(num_q):
    name_q = query[q_idx][0].split('/')[-1]
    order = indices[q_idx]  # 该query id索引对应的gallery索引
    # name_g = []
    i = 0
    for g_idx in order:
        name_g_idx = gallery[g_idx][0].split('/')[-1]
        # name_g.append(name_g_idx)
        index_dic[name_q].append(name_g_idx)
        i += 1
        if i == 200:  # 只保存前200个
            break
with open('../json_save/submit.json','w') as file:
    json.dump(index_dic,file)
    file.close()